<?php
class qFront
{
	private static $basedir;
	private static $modrewrite = true;
	
	public static function DisableModRewrite()
	{
	    self::$modrewrite = false;
	}
	
	public static function SetBaseDir($dir)
	{
		if ($dir[0] != '/') $dir = '/' . $dir;
		if ($dir[strlen($dir) - 1] == '/') $dir = substr($dir, 0, -1);
		self::$basedir = $dir;
	}
	
    public static function Run()
    {
    	qLoader::LoadLibrary("globals");
    	qGlobals::Init();    
    	
		$request = qGlobals::Server("REQUEST_URI");
		$len = strlen(self::$basedir) + (self::$modrewrite ? 0 : 2);
		$request = substr($request, $len, strlen($request) - $len);
		
		list($dummy, $controller, $request) = explode('/', $request, 3);
		
		if ($controller == '')
			$controller = "index";
		if ($request == '')
			$request = "/index";
		
		if ($module = qLoader::GetModule($controller))
		{
		    $module->Module()->Run($request);
		}
		else if ($controller = qLoader::Controller($controller))
		{
		    $controller->Run($request);
		}
		else
		{
		    qHeader->Location("/error/404");
		}
		if ($controller == "error") die("<h1>ERROR 404</h1>");
		header('Location: '.self::$basedir.'/error/404/');
    }
}